﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BCCommon;
using System.Text;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using BCDto.Trading.SaleGoods.SaleGoods;
using BCTMSRESTful.Configs;
using BCService.Utils;
using BCService.Trading.SaleGoods.SaleGoods;
using BCDto.Trading.SaleGoods.SaleGoodsNorm;

namespace BCTMSRESTful.Controllers.Management.Trading.SaleGoods
{
    /// <summary>
    /// 销售商品
    /// </summary>
    [ApiExplorerSettings(GroupName = "AdminTrading")]
    [Route("api/v1/sim/[controller]")]
    public class SaleGoodsController : ManagementBaseController
    {
        private readonly ISaleGoodsService saleGoodsService;

        public SaleGoodsController(ISaleGoodsService saleGoodsService)
        {
            this.saleGoodsService = saleGoodsService;
        }

        #region 销售商品相关接口
        #region 添加销售商品
        /// <summary>
        ///  添加销售商品
        /// </summary>
        /// <param name="requestDto">销售商品信息</param>
        /// <response code="201">添加销售商品成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加销售商品")]
        [ProducesResponseType(typeof(SaleGoodsDto), 201)]
        public IActionResult Post(
            [FromBody]SaleGoodsRequestDto requestDto)
        {
            var result = this.saleGoodsService.Add(requestDto);
            return StatusCode((int)HttpStatusCode.Created, result);
        }
        #endregion

        #region 修改销售商品
        /// <summary>
        ///  修改销售商品
        /// </summary>
        /// <param name="saleGoodsId">销售商品Id</param>
        /// <param name="putDto">销售商品信息</param>
        /// <response code="200">修改销售商品成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("{sale_goods_id}")]
        [Log("修改销售商品")]
        [ProducesResponseType(typeof(SaleGoodsDto), 200)]
        public IActionResult Put([FromRoute(Name = "sale_goods_id")]long saleGoodsId, [FromBody]SaleGoodsPutDto putDto)
        {
            putDto.SaleGoodsId = saleGoodsId;
            return StatusCode((int)HttpStatusCode.OK, this.saleGoodsService.Update(putDto));
        }
        #endregion

        #region 删除销售商品
        /// <summary>
        ///  删除销售商品
        /// </summary>
        /// <param name="saleGoodsId">销售商品Id</param>
        /// <response code="204">删除销售商品成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpDelete("{sale_goods_id}")]
        [Log("删除销售商品")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Delete([FromRoute(Name = "sale_goods_id")]long saleGoodsId)
        {
            this.saleGoodsService.Delete(saleGoodsId);
            return StatusCode((int)HttpStatusCode.NoContent);
        }
        #endregion

        #region 获取销售商品
        /// <summary>
        ///  获取销售商品
        /// </summary>
        /// <param name="saleGoodsId">销售商品Id</param>
        /// <response code="200">获取销售商品</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("{sale_goods_id}")]
        [ProducesResponseType(typeof(SaleGoodsDto), 200)]
        public IActionResult Get([FromRoute(Name = "sale_goods_id")]long saleGoodsId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.saleGoodsService.Get(saleGoodsId));
        }
        #endregion

        #region 获取销售商品列表
        /// <summary>
        ///  获取销售商品列表
        /// </summary>
        /// <response code="200">获取销售商品列表</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SaleGoodsDto), 200)]
        public IActionResult Get()
        {
            return StatusCode((int)HttpStatusCode.OK, this.saleGoodsService.GetList());
        }
        #endregion

        #region 获取销售商品（分页）
        /// <summary>
        ///  获取销售商品（分页）
        /// </summary>
        /// <param name="saleGoodsCategoryId">商品分类Id</param>
        /// <param name="categoryName">商品分类名称</param>
        /// <param name="goodsName">商品名称</param>
        /// <param name="sku">商品SKU</param>
        /// <param name="enabled">是否启用</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">升序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">当前页大小</param>
        /// <response code="200">获取销售商品（分页）</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<SaleGoodsDto>), 200)]
        public IActionResult Query(
            [FromQuery(Name = "sale_goods_category_id")]long? saleGoodsCategoryId,
            [FromQuery(Name = "category_name")]string categoryName,
            [FromQuery(Name = "goods_name")]string goodsName,
            [FromQuery(Name = "sku")]string sku,
            [FromQuery(Name = "enabled")]bool? enabled,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "sort_name")]string sortName,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return StatusCode((int)HttpStatusCode.OK, this.saleGoodsService.Query(saleGoodsCategoryId, categoryName, goodsName, sku, enabled, start, end, sortName, ascending, index, size));
        }
        #endregion

        #region 启用/禁用销售商品
        /// <summary>
        ///  启用/禁用销售商品
        /// </summary>
        /// <param name="saleGoodsId">销售商品Id</param>
        /// <param name="enabled">是否启用</param>
        /// <response code="204">启用/禁用销售商品成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("[action]/{sale_goods_id}/{enabled}")]
        [Log("启用/禁用销售商品")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Enabled([FromRoute(Name = "sale_goods_id")]long saleGoodsId,
            [FromRoute(Name = "enabled")]bool enabled)
        {
            if (enabled)
            {
                this.saleGoodsService.Enabled(saleGoodsId);
                return StatusCode((int)HttpStatusCode.NoContent);
            }
            else
            {
                this.saleGoodsService.Disabled(saleGoodsId);
                return StatusCode((int)HttpStatusCode.NoContent);
            }
        }
        #endregion
        #endregion

        #region 销售商品规格相关接口
        #region 添加销售商品规格
        /// <summary>
        ///  添加销售商品规格
        /// </summary>
        /// <param name="requestDto">销售商品规格信息</param>
        /// <response code="201">添加销售商品规格成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPost("GoodsNorm")]
        [Log("添加销售商品规格")]
        [ProducesResponseType(typeof(SaleGoodsNormDto), 201)]
        public IActionResult Post(
            [FromBody]SaleGoodsNormRequestDto requestDto)
        {
            var result = this.saleGoodsService.AddGoodsNorm(requestDto);
            return StatusCode((int)HttpStatusCode.Created, result);
        }
        #endregion

        #region 修改销售商品规格
        /// <summary>
        ///  修改销售商品规格
        /// </summary>
        /// <param name="saleGoodsNormId">销售商品规格Id</param>
        /// <param name="requestDto">销售商品规格信息</param>
        /// <response code="200">修改销售商品规格成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("GoodsNorm/{sale_goods_norm_id}")]
        [Log("修改销售商品规格")]
        [ProducesResponseType(typeof(SaleGoodsNormDto), 200)]
        public IActionResult Put([FromRoute(Name = "sale_goods_norm_id")]long saleGoodsNormId, [FromBody]SaleGoodsNormRequestDto requestDto)
        {
            SaleGoodsNormPutDto putDto = requestDto.As<SaleGoodsNormPutDto>();
            putDto.SaleGoodsNormId = saleGoodsNormId;
            return StatusCode((int)HttpStatusCode.OK, this.saleGoodsService.UpdateGoodsNorm(putDto));
        }
        #endregion

        #region 删除销售商品规格
        /// <summary>
        ///  删除销售商品规格
        /// </summary>
        /// <param name="saleGoodsNormId">销售商品规格Id</param>
        /// <response code="204">删除销售商品规格成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpDelete("GoodsNorm/{sale_goods_norm_id}")]
        [Log("删除销售商品规格")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult DeleteGoodsNorm([FromRoute(Name = "sale_goods_norm_id")]long saleGoodsNormId)
        {
            this.saleGoodsService.DeleteGoodsNorm(saleGoodsNormId);
            return StatusCode((int)HttpStatusCode.NoContent);
        }
        #endregion

        #region 获取销售商品规格
        /// <summary>
        ///  获取销售商品规格
        /// </summary>
        /// <param name="saleGoodsNormId">销售商品规格Id</param>
        /// <response code="200">获取销售商品规格</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("GoodsNorm/{sale_goods_norm_id}")]
        [ProducesResponseType(typeof(SaleGoodsNormDto), 200)]
        public IActionResult GetGoodsNorm([FromRoute(Name = "sale_goods_norm_id")]long saleGoodsNormId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.saleGoodsService.GetGoodsNorm(saleGoodsNormId));
        }
        #endregion

        #region 获取销售商品规格列表
        /// <summary>
        ///  获取销售商品规格列表
        /// </summary>
        /// <param name="saleGoodsId">商品Id</param>
        /// <response code="200">获取销售商品规格列表</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("GoodsNorms/{sale_goods_id}")]
        [ProducesResponseType(typeof(IEnumerable<SaleGoodsNormDto>), 200)]
        public IActionResult GetGoodsNormList([FromRoute(Name ="sale_goods_id")]long saleGoodsId)
        {
            return StatusCode((int)HttpStatusCode.OK, this.saleGoodsService.GetGoodsNormList(saleGoodsId));
        }
        #endregion

        #region 获取销售商品规格（分页）
        /// <summary>
        ///  获取销售商品规格（分页）
        /// </summary>
        /// <param name="categoryName">分类名称</param>
        /// <param name="goodsName">商品名称</param>
        /// <param name="sku">SKU</param>
        /// <param name="enabled">是否启用</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">升序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">当前页大小</param>
        /// <response code="200">获取销售商品规格（分页）</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("GoodsNorm/Query")]
        [ProducesResponseType(typeof(PageableList<SaleGoodsNormDto>), 200)]
        public IActionResult QueryGoodsNorm(
            [FromQuery(Name = "category_name")]string categoryName,
            [FromQuery(Name = "goods_name")]string goodsName,
            [FromQuery(Name = "sku")]string sku,
            [FromQuery(Name = "enabled")]bool? enabled,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "sort_name")]string sortName,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index = 1,
            [FromQuery(Name = "size")]int size = 10)
        {
            return StatusCode((int)HttpStatusCode.OK, this.saleGoodsService.QueryGoodsNorm(sku, categoryName, goodsName,  enabled, start, end, sortName, ascending, index, size));
        }
        #endregion

        #region 启用/禁用销售商品规格
        /// <summary>
        ///  启用/禁用销售商品规格
        /// </summary>
        /// <param name="saleGoodsNormId">销售商品规格Id</param>
        /// <param name="enabled">是否启用</param>
        /// <response code="204">启用/禁用销售商品规格成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("GoodsNorm/enabled/{sale_goods_norm_id}/{enabled}")]
        [Log("启用/禁用销售商品规格")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult EnabledGoodsNorm([FromRoute(Name = "sale_goods_norm_id")]long saleGoodsNormId,
            [FromRoute(Name = "enabled")]bool enabled)
        {
            if (enabled)
            {
                this.saleGoodsService.EnabledGoodsNorm(saleGoodsNormId);
                return StatusCode((int)HttpStatusCode.NoContent);
            }
            else
            {
                this.saleGoodsService.DisabledGoodsNorm(saleGoodsNormId);
                return StatusCode((int)HttpStatusCode.NoContent);
            }
        }
        #endregion
        #endregion
    }
}

